import { ajax } from "../../utils/ajax";
const tbody = document.querySelector('tbody');
const manipulate = document.getElementById('manipulate');
const pageBar = document.getElementById('pageBar');

//分页要求
let pageCurrent = 1;
//每页显示文档数
let pageSize = 7;
//总页数
let pageTotal;

//修改表格时间显示格式
Date.prototype.format = function (string = 'yyyy-MM-dd hh:mm:ss') {
    string = string.replace('yyyy', this.getFullYear());
    string = string.replace('MM', this.getMonth() + 1);
    string = string.replace('dd', this.getDate());
    string = string.replace('hh', this.getHours());
    string = string.replace('mm', this.getMinutes());
    string = string.replace('ss', this.getSeconds());
    string = string.replace('ms', this.getMilliseconds());
    string = string.replace('ww', this.getDay());

    return string;
}
//执行渲染
listRender();
//请求判断超管权限,渲染管理员列表
function listRender() {
    ajax.get('get', '/api/admin/verifySuperAdmin', '', localStorage.getItem('token')).then(result => {
        //是超管权限,请求获取超管的管理员列表
        if (result.flag && result.data.level == 0) {
            ajax.get('get', '/api/admin/listAdmin', { pageCurrent, pageSize }, localStorage.getItem('token')).then(result => {
                if (result.flag) {
                    //遍历数据库数据,循环渲染至页面
                    console.log(result.data)
                    result.data.forEach(item => {
                        let tr = document.createElement('tr');
                        let td;
                        td = document.createElement('td');
                        td.innerHTML = `<img src=http://127.0.0.1:3000/${item.avatar}>`;
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerText = item.username;
                        tr.appendChild(td);

                        td = document.createElement('td');
                        if (item.level == 0) {
                            td.innerText = 'SuperAdmin';
                        } else if (item.level == 1) {
                            td.innerText = 'Admin';
                        }
                        // td.innerText = item.level;
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerText = new Date(item.createdAt).format('yyyy-MM-dd hh:mm:ss');
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerText = new Date(item.updatedAt).format('yyyy-MM-dd hh:mm:ss');
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerHTML = `<a class="caozuo_td" id="deleteAdmin" data-username=${item.username} data-type=remove>删除</a>`;
                        tr.appendChild(td);

                        tbody.appendChild(tr);
                    })
                    //给td做事件委托
                    const caozuoArr = document.getElementsByClassName('caozuo_td');
                    for (let item of caozuoArr) {
                        item.onclick = caozuo_click;
                    }
                    pageTotal = result.pageTotal;
                    //渲染分页栏
                    page_bar(result.pageTotal);

                } else {
                    alert(result.msg);
                }
            })
        } else if (result.flag) {
            //获取普通管理员列表
            ajax.get('get', '/api/admin/listAdmin', { pageCurrent, pageSize }, localStorage.getItem('token')).then(result => {
                if (result.flag) {
                    //删除操作栏
                    manipulate.remove();
                    //遍历数据库数据,循环渲染至页面
                    result.data.forEach(item => {
                        let tr = document.createElement('tr');
                        let td;
                        td = document.createElement('td');
                        td.innerHTML = `<img src=http://127.0.0.1:3000/${item.avatar}>`;
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerText = item.username;
                        tr.appendChild(td);

                        td = document.createElement('td');
                        if (item.level == 0) {
                            td.innerText = 'SuperAdmin';
                        } else if (item.level == 1) {
                            td.innerText = 'Admin';
                        }
                        // td.innerText = item.level;
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerText = new Date(item.createdAt).format('yyyy-MM-dd hh:mm:ss');
                        tr.appendChild(td);

                        td = document.createElement('td');
                        td.innerText = new Date(item.updatedAt).format('yyyy-MM-dd hh:mm:ss');
                        tr.appendChild(td);

                        tbody.appendChild(tr);
                    })
                    pageTotal = result.pageTotal;
                    //渲染分页栏
                    page_bar(result.pageTotal);
                } else {
                    alert(result.msg);
                }
            })
        } else {
            alert(result.msg);
        }
    })
}


//删除管理员
function caozuo_click(e) {
    if (e.target.dataset.type == 'remove') {
        console.log(e.target.dataset.username);
        if (confirm('确定删除此项数据吗？')) {
            ajax.get('get', '/api/admin/deleteAdmin', { username: e.target.dataset.username }, localStorage.getItem('token')).then(result => {
                if (result.flag) {
                    alert(result.msg);
                    location.reload();
                } else {
                    alert(result.msg);
                }
            })
        }
    }
}
//设置分页栏
function page_bar(pageTotal) {
    let a = document.createElement('a');
    a.innerText = '首页';
    a.dataset.index = 1;
    pageBar.appendChild(a);

    a = document.createElement('a');
    a.innerText = '上一页';
    a.dataset.index = `${pageCurrent - 1}`;
    pageBar.appendChild(a);

    for (let i = 1; i <= pageTotal; i++) {
        a = document.createElement('a');
        a.innerText = i;
        a.dataset.index = i;
        pageBar.appendChild(a);
    }

    a = document.createElement('a');
    a.innerText = '下一页';
    a.dataset.index = `${pageCurrent + 1}`;
    pageBar.appendChild(a);

    a = document.createElement('a');
    a.innerText = '末页';
    a.dataset.index = `${pageTotal}`;
    pageBar.appendChild(a);

    //选中当前页设置背景色，注意data-index属性的值是字符串
    [...document.querySelectorAll(`[data-index='${pageCurrent}']`)].forEach(item => {
        item.classList.add('pi');
    })
    //绑定页数点击事件委托,
    pageBar.onclick = pageBar_click;
}
function pageBar_click(e) {
    if (e.target.nodeName == 'A') {
        let index = e.target.dataset.index;
        if (index < 1) {
            alert('已经是首页');
            return;
        } else if (index > pageTotal) {
            alert('已经是末页');
            return;
        } else {
            //清空页面
            tbody.innerHTML = '';
            pageBar.innerHTML = '';
            //重新赋值当前页
            pageCurrent = e.target.dataset.index * 1;
            listRender();
        }
    }
}
